import type { App, Directive, DirectiveBinding } from 'vue';

import { AUTH_CODE_LIST } from '@/enums/app-enum';
import { getAuthCache } from '@/utils/auth';

function check(authCode: any) {
  return (getAuthCache(AUTH_CODE_LIST) || []).includes(authCode);
}

const mounted = (el: Element, binding: DirectiveBinding) => {
  if (!check(binding.value)) {
    el.parentNode && el.parentNode.removeChild(el);
  }
};

const authDirective: Directive = {
  mounted
};

export function setupPermissionDirective(app: App) {
  app.directive('auth', authDirective);
}
